package com.leonwoodstudio.eventdetectionclient.utils;

import android.content.Context;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * Created by win7 on 2017/3/16.
 */
public class Util {
    public static final String PROPERTIES_KEY_REMOTE_PORT = "remote_port";
    public static final String PROPERTIES_KEY_REMOTE_IP_ADDRESS = "remote_ip_address";

    public static Properties getProperties(Context context){
        Properties props = new Properties();
        try {
            //方法一：通过activity中的context攻取setting.properties的FileInputStream
            InputStream is = context.getAssets().open("config.properties");
            //方法二：通过class获取setting.properties的FileInputStream
            //InputStream is = Util.class.getResourceAsStream("/assets/config.properties");
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            props.load(br);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return props;
    }

    public static Properties getPropertiesFromExtFileIfExist(){
        File saved_config = new File(FileAccessor.getInstance(null).getFilesDir() + "config.properties");
        if (!saved_config.exists()) {
            return null;
        }
        InputStream is = null;
        try {
            Properties props = new Properties();
            is = new FileInputStream(saved_config);
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            props.load(br);
            return props;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void saveProperties(Properties props) {
        File to_save_config = new File(FileAccessor.getInstance(null).getFilesDir() + "config.properties");
        OutputStream os;
        BufferedWriter bw = null;
        try {
            os = new FileOutputStream(to_save_config);
            bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            props.store(bw, null);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static Date StringToDate(String str) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmSS");
        try {
            Date date = fmt.parse(str);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String DateToString(Date date) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        return fmt.format(date);
    }
}
